package com.eeda123.jfinal.handler;

import com.eeda123.jfinal.route.DynamicRouteController;
import com.jfinal.handler.Handler;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.RenderManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DynamicRouteHandler extends Handler {

	@Override
	public void handle(String target, HttpServletRequest request,
			HttpServletResponse response, boolean[] isHandled) {
		LogKit.debug("handle url: " + target);

		//如果是带有.符号的, 就是直接请求文件(.html, .jpg, .css), 不处理
		if(target.indexOf(".")==-1) {
			// actionMapping 根据 target 得到对应的 action
			Record rec = Db.findFirst("select * from t_dynamic_route where url=? and is_delete != 'Y'", target);
			if (rec != null) {
				LogKit.debug("动态, 执行动态配置的代码");
				request.setAttribute("originUrl", target);
				target = target.replaceAll("/", "-").substring(1);
				target = DynamicRouteController.DEFAULT_ROUTE+ "/" + target;
			} else {

				LogKit.debug("非动态, 正常执行controller...");
			}
		}
		// logger.debug("content path:"+target);
		next.handle(target, request, response, isHandled);
	}
}
